﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using S4Store.Persistent;
using S4Store.Domain;
using LX.Core.Query;
using S4Store.Service.Valid.StoreValid;

namespace S4Store.Service
{
    public class EmployeeService : BaseService
    {
        public S4Store.Service.Valid.SellerValid.Add ValidAdd { get; set; }
        public S4Store.Service.Valid.SellerValid.Modify ValidModify { get; set; }
        public S4Store.Service.Valid.SellerValid.Delete ValidDelete { get; set; }

        public IQueryable<Employee> GetQuery()
        {
            return this.GetQuery<Employee>();
        }
        public IQueryable<Employee> GetQuery(EmployeeType type)
        {
            int typevalue = type.ToInt();
            return this.GetQuery<Employee>().Where(t => (t.TypeValue & typevalue) == t.TypeValue);
        }
        public IQueryable<Employee> GetEnableEmployee(int storeid)
        {
            int i=Status.NORMAL.ToInt();
            return this.GetQuery<Employee>().Where(t=>t.StoreId==storeid&&!t.IsClose&&t.StatusValue==i );
        }

        public IQueryable<Employee> GetEnableEmployee(int storeid, EmployeeType type)
        {
            int typevalue = type.ToInt();
            int i = Status.NORMAL.ToInt();
            return this.GetQuery<Employee>().Where(t => t.StoreId == storeid && !t.IsClose && t.StatusValue == i && (t.TypeValue & typevalue) == t.TypeValue);
        }
        public void AddEmployee(Employee model)
        {
            this.Valid(model, ValidAdd);
            model.Name = model.Name.Trim();
            this.GetSet<Employee>().Add(model);
        }


        public void ModifyEmployee(Employee model)
        {
            Employee oldmodel = GetQuery().First(t => t.Id == model.Id);
            oldmodel.Sex = model.Sex;
            oldmodel.StoreId = model.StoreId;
            oldmodel.WorkStartDate = model.WorkStartDate;
            oldmodel.Name = model.Name.Trim();
            oldmodel.Tel = model.Tel;
            oldmodel.Birthday = model.Birthday;
            oldmodel.SellerGrade = model.SellerGrade;
            oldmodel.Status = model.Status;            
            this.Valid(oldmodel, ValidModify);
        }

        public void DeleteEmployee(int id)
        {
            Employee model = GetQuery().First(t => t.Id == id);
            if (model == null) return;
            this.Valid(model, ValidDelete);
            this.GetSet<Employee>().Remove(model);
        }
        
         
    }
}
